package com.example.controll.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.config.R;
import com.example.domain.user;
import com.example.service.user.insertUserservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/UserQuery")
public class userControll {

    @Autowired
    private insertUserservice insertUserservice;

    @RequestMapping("/insert")
    private R Insert(user users){
        System.out.println(users.getName());
        System.out.println(users.getSex());
        System.out.println(users.getOpenid());
        System.out.println("aaaaa");

        String openId=users.getOpenid();
        openId="'"+openId+"'";
        boolean flag=false;
        if (insertUserservice.getByOpenId(openId)==null){
            flag=insertUserservice.insert(users);

        }
        return new R(flag,flag?"添加成功":"添加失败");
    }

    @RequestMapping("/updata")
    private R Updata(user users){

        String a ="'"+users.getOpenid()+"'";
        System.out.println("更新");
        user byOpenId = insertUserservice.getByOpenId(a);

        users.setId(byOpenId.getId());
        boolean flag = insertUserservice.updata(users);
        return new R(flag, flag ? "修改成功^_^" : "修改失败-_-!");
    }

    @RequestMapping("/select")
    private R findByopenid(String openid){
        openid="'"+openid+"'";
        return new R(true,insertUserservice.getByOpenId(openid));
    }

    @RequestMapping("/findAll")
    private R all(int a,int b){
        System.out.println("aaaaa");
        int allnumber = insertUserservice.allnumber();
        System.out.println(allnumber);
        return  new R(true,insertUserservice.selectall(a,b));
    }

    @RequestMapping("/number")
    private R allnumber(){
        return new R(true,insertUserservice.allnumber());
    }

    @RequestMapping("/findbyusername")
    private R findbyname(user user,int a,int b){
        IPage<com.example.domain.user> page = insertUserservice.getPagebyname(a, b, user);
        if(a>page.getPages()){
            page = insertUserservice.getPagebyname((int) page.getPages(), b, user);
        }
        return new R(true,page);
    }



}
